"""
多层感知机
常见激活函数
"""

import tensorflow as tf
from d2l import tensorflow as d2l

if __name__=='__main__':
    x = tf.Variable(tf.range(-8.0, 8.0, 0.1), dtype=tf.float32)
    # relu函数
    y = tf.nn.relu(x)
    d2l.plot(x.numpy(), y.numpy(), 'x', 'relu(x)', figsize=(5,2.5))
    d2l.plt.show()
    with tf.GradientTape() as t:
        y = tf.nn.relu(x)
        #relu求导
        d2l.plot(x.numpy(), t.gradient(y, x).numpy(), 'x', 'grad of relu',
            figsize=(5,2.5))
        d2l.plt.show()
    # sigmoid函数
    y = tf.nn.sigmoid(x)
    d2l.plot(x.numpy(), y.numpy(), 'x', 'sigmoid(x)',figsize=(5,2.5))
    d2l.plt.show()
    with tf.GradientTape() as t:
        y = tf.nn.sigmoid(x)
        #sigmoid导数
        d2l.plot(x.numpy(), t.gradient(y, x).numpy(), 'x', 'grad of sigmoid',
                figsize=(5,2.5))
        d2l.plt.show()
    #tanh函数
    y = tf.nn.tanh(x)
    d2l.plot(x.numpy(), y.numpy(), 'x', 'tanh(x)', figsize=(5,2.5))
    d2l.plt.show()
    with tf.GradientTape() as t:
        y = tf.nn.tanh(x)
        d2l.plot(x.numpy(), t.gradient(y,x).numpy(), 'x', 'grad of tanh',
                figsize=(5, 2.5))
        d2l.plt.show()

